/*
 * Copyright 2017 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package com.zhongan.gateway.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zhongan.gateway.bean.Api;
import com.zhongan.gateway.bean.ApiGroup;
import com.zhongan.gateway.bean.Developer;
import com.zhongan.gateway.service.ApiService;
import com.zhongan.gateway.service.DeveloperService;

/**
 * 类IndexController.java的实现描述：Index请求
 * 
 * @author wujian 2017年4月5日 下午2:45:36
 */
@Controller
public class IndexController {
    private static final Logger LOG = LoggerFactory.getLogger(IndexController.class);

    @Resource
    private ApiService          apiService;

    @Resource
    private DeveloperService    developerService;

    @RequestMapping(value = "/index")
    public String welcome(Map<String, Object> model, HttpServletRequest request) {
        return "screen/gateway/searchApi";
    }

    //    @RequestMapping(value = "/")
    //    public String managerDefaultView(Map<String, Object> model, HttpServletRequest request) {
    //        return "screen/gateway/searchApi";
    //    }

    @RequestMapping(value = "/api/searchApi")
    public String apiView(Map<String, Object> model, HttpServletRequest request) {

        return "screen/gateway/searchApi";
    }

    @RequestMapping(value = "/api/apiGroup")
    public String apiGroupView(Map<String, Object> model, HttpServletRequest request) {
        return "screen/gateway/apiGroup";
    }

    @RequestMapping(value = "/api/addApi")
    public String addApiView(Map<String, Object> model, HttpServletRequest request) {
        return "screen/gateway/addApi";
    }

    @RequestMapping(value = "/api/editApi")
    public ModelAndView editApiView(Map<String, Object> model, HttpServletRequest request) {
        String id = request.getParameter("id");
        Api api = apiService.selectApiById(Long.parseLong(id));
        List<ApiGroup> list = apiService.selectGroups();
        ModelAndView mav = new ModelAndView();
        mav.addObject("api", api);
        mav.addObject("groupList", list);
        mav.setViewName("screen/gateway/editApi");
        return mav;
    }

    @RequestMapping(value = "/dev/searchDev")
    public String searchDevView(HttpServletRequest request) {
        return "screen/gateway/searchDev";
    }

    @RequestMapping(value = "/log/searchLog")
    public String searchLogView(HttpServletRequest request) {
        return "screen/gateway/searchLog";
    }

    @RequestMapping(value = "/dev/addDev")
    public ModelAndView addDevView(HttpServletRequest request) {
        String id = request.getParameter("id");
        ModelAndView mav = new ModelAndView();
        if (!StringUtils.isEmpty(id)) {
            Developer dev = developerService.selectDeveloperById(Long.parseLong(id));
            mav.addObject("dev", dev);
        }

        mav.setViewName("screen/gateway/addDev");
        return mav;
    }

    @RequestMapping(value = "/dev/searchIp")
    public ModelAndView searchIpView(HttpServletRequest request) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        ModelAndView mav = new ModelAndView();
        mav.addObject("developerId", id);
        mav.addObject("devName", name);

        mav.setViewName("screen/gateway/searchIp");
        return mav;
    }

    @RequestMapping(value = "/dev/searchPri")
    public ModelAndView searchPriView(HttpServletRequest request) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        ModelAndView mav = new ModelAndView();
        mav.addObject("developerId", id);
        mav.addObject("devName", name);
        List<Api> list = apiService.selectApis(new Api());
        mav.addObject("allApis", list);
        mav.setViewName("screen/gateway/searchPrivilege");
        return mav;
    }

    @RequestMapping(value = "/api/addAttr")
    public ModelAndView addAttrView(HttpServletRequest request) {
        String apiId = request.getParameter("apiId");
        String serviceName = request.getParameter("serviceName");
        ModelAndView mav = new ModelAndView();
        mav.addObject("apiId", apiId);
        mav.addObject("serviceName", serviceName);

        mav.setViewName("screen/gateway/addAttr");
        return mav;
    }

    @RequestMapping(value = "/api/apiCache")
    public String apiCacheView(HttpServletRequest request) {

        return "screen/gateway/apiCache";
    }

    @RequestMapping(value = "/401")
    public String error401(Map<String, Object> model, HttpServletRequest request) {
        model.put("errorCode", "401");
        LOG.info("error-401");
        return "error";
    }

    @RequestMapping(value = "/404")
    public String error404(Map<String, Object> model, HttpServletRequest request) {
        model.put("errorCode", "404");
        LOG.info("error-404");
        return "error";
    }

    @RequestMapping(value = "/500")
    public String error500(Map<String, Object> model, HttpServletRequest request) {
        model.put("errorCode", "500");
        LOG.info("error-500");
        return "error";
    }

}
